Page({
  data:{
    "hospitalname":"",
    "hospital":{},
    "urlprefix":"http://tnfs.tngou.net/img",
    "isFavorite":false,
    "desprition":""
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({"hospitalname":options.hospitalname})
    this.getHospitalDetail()
    try {
        var value = wx.getStorageSync('collectionHospital')
         if (value) {
             var array = []
             array = value
             var index = -1 
              for(var i = 0;i < array.length;i++){
                if(array[i] == options.hospitalname){
                    index = i
                }
              }
              if(index > -1){
                  this.setData({"isFavorite":true})
              }
          }
        } catch (e) {
        // Do something when catch error
          }
  },
  onReady:function(){
    // 页面渲染完成
    
  },
  onShow:function(){
    // 页面显示
    
  },
  onHide:function(){
    // 页面隐藏
    
  },
  onUnload:function(){
    // 页面关闭
    
  },
  //得到医院信息详情
  getHospitalDetail:function(){
    wx.showToast({title:"加载中...",icon:"loading",duration:10000})
    var that = this
    wx.request({
      url: 'https://apis.baidu.com/tngou/hospital/name',
      data: {
        "name":that.data.hospitalname
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
          "apikey":"da9ab87de033d3b01b34e35d5bfac404"
      }, // 设置请求的 header
      success: function(res){
        that.setData({"hospital":res.data,"desprition":res.data.message})
        console.log("-----"+res.data)
          that.replaceMessage(that.data.desprition)
      },
      fail: function() {
        // fail
      },
      complete: function() {
        wx.hideToast()
      }
    })
  },
  //拨打电话
  makeTel:function(){
    var telNum = this.data.hospital.tel
    if(telNum == ""){
      return
    }else{
      wx.showModal({
      title: '提示',
      content: '是否拨打电话'+telNum,
      success: function(res) {
      if (res.confirm) {
        wx.makePhoneCall({"phoneNumber":telNum})
        }
      }
      })
    }
  },
  favorite:function(){
    var that = this
    this.setData({"isFavorite":!this.data.isFavorite})
    if(this.data.isFavorite){
        this.saveHospital()
    }else{
        this.removeHospital()
    }
    
  },
  saveHospital:function(){
    var hospatialname = this.data.hospitalname
    var colletionArray = []
    try {
       var value = wx.getStorageSync('collectionHospital')
         if (value) {
            colletionArray = value
        }
      } catch (e) {
        console.log("读取缓存失败")
      }
      colletionArray.push(hospatialname)
    wx.setStorage({
      key: 'collectionHospital',
      data: colletionArray,
      success: function(res){
        // success
        console.log("缓存成功" + res + "数组"+ colletionArray)
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  removeHospital:function(){
      var hospatialname = this.data.hospitalname
    var colletionArray = []
    try {
       var value = wx.getStorageSync('collectionHospital')
         if (value) {
            colletionArray = value
        }
      } catch (e) {
        console.log("读取缓存失败")
      }
      var index = -1
      for (var i = 0;i < colletionArray.length;i++){
        if(colletionArray[i] == hospatialname){
          index = i
        }
      }
      if(index > -1 ){
        colletionArray.splice(index,1)
      }
    wx.setStorage({
      key: 'collectionHospital',
      data: colletionArray,
      success: function(res){
        // success
        console.log("缓存成功" + res + "数组"+ colletionArray)
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })

  },
  replaceMessage:function(str){
    var newStr = str
    while(newStr.indexOf("</p>") >= 0){
        newStr = newStr.replace('</p>','\n')
    }
    while(newStr.indexOf("<p>") >= 0){
        newStr = newStr.replace('<p>','')
    }
    this.setData({"desprition":newStr})
      console.log("---" + newStr)
  }
})